package com.hy.mybatisplus.entity;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * @author hy
 * @date 2020/09/29  9:30 AM
 */

@Service
public class R extends HashMap<String, Object> {

    public R() {
        put("code", 0);
    }

    public R(Integer code) {
        put("code", code);
        put("data", new HashMap<String, Object>());
    }

    public R(Integer code, String msg) {
        put("code", code);
        put("msg", msg);
        put("data", new HashMap<String, Object>());
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }


    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R().put("msg", "success").put("data", new HashMap<String, Object>());
    }

    public static R ok(Integer size) {
        return new R().put("data", new HashMap<String, Object>((int) Math.round(size / 0.75)));
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 添加返回结果数据
     *
     * @param key
     * @param value
     * @return
     */
    public R putData(String key, Object value) {
        Map<String, Object> map = (HashMap<String, Object>) this.get("data");
        map.put(key, value);
        return this;
    }


}
